﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Admin1_Default : System.Web.UI.Page
{
    //Datacon db = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //db.ecDropDownList(ddlCourse, "select * from Course where teacher_id='" + Session["ID"] + "'", "c_name", "c_id");
        }
        ImageButton2.Visible = false;

        Rscore();
        double num = Convert.ToDouble(Label21.Text);

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {

            double num = Convert.ToDouble(Label21.Text);
            if (num <= 100)
            {
                //string GridView1Str = "select top " + int.Parse(SingleNum.Text.Trim()) + " * from SingleProblem where c_id='" + ddlCourse.SelectedValue + " ' order by newid()";//根据参数设置查询单选题Sql语句
                //DataSet ds1 = db.GetDataSetSql(GridView1Str);//调用DataBase类方法GetDataSetSql方法查询数据
                //GridView1.DataSource = ds1.Tables[0].DefaultView;//为单选题GridView控件指名数据源
                GridView1.DataSource = (from q in ds.SingleProblem
                                        where q.c_id == Convert.ToInt32(ddlCourse.SelectedValue)
                                        select q).Take(int.Parse(SingleNum.Text.Trim()));
                GridView1.DataBind();//绑定数据

                //string GridView2Str = "select top " + int.Parse(MultiNum.Text.Trim()) + " * from MultiProblem where c_id='" + ddlCourse.SelectedValue + " ' order by newid()";//根据参数设置查询多选题Sql语句
                //DataSet ds2 = db.GetDataSetSql(GridView2Str);//调用DataBase类方法GetDataSetSql方法查询数据
                //GridView2.DataSource = ds2.Tables[0].DefaultView;//为多选题GridView控件指名数据源
                GridView2.DataSource = (from q in ds.MultiProblem
                                        where q.c_id == Convert.ToInt32(ddlCourse.SelectedValue)
                                        select q).Take(int.Parse(MultiNum.Text.Trim()));
                GridView2.DataBind();//绑定数据

                //string GridView3Str = "select top " + int.Parse(JudgeNum.Text.Trim()) + " * from JudgeProblem  where c_id='" + ddlCourse.SelectedValue + " ' order by newid()";//根据参数设置查询判断题Sql语句
                //DataSet ds3 = db.GetDataSetSql(GridView3Str);//调用DataBase类方法GetDataSetSql方法查询数据
                //GridView3.DataSource = ds3.Tables[0].DefaultView;//为判断题GridView控件指名数据源
                GridView3.DataSource = (from q in ds.JudgeProblem
                                        where q.c_id == Convert.ToInt32(ddlCourse.SelectedValue)
                                        select q).Take(int.Parse(JudgeNum.Text.Trim()));
                GridView3.DataBind();//绑定数据

                GridView4.DataSource = (from q in ds.JianDaProblem
                                        where q.c_id == Convert.ToInt32(ddlCourse.SelectedValue)
                                        select q).Take(int.Parse(JianDaNum.Text.Trim()));
                GridView4.DataBind();//绑定数据

                GridView5.DataSource = (from q in ds.LunShuProblem
                                        where q.c_id == Convert.ToInt32(ddlCourse.SelectedValue)
                                        select q).Take(int.Parse(LunShuNum.Text.Trim()));
                GridView5.DataBind();//绑定数据

                ImageButton2.Visible = true;
            }
            else
            {
                Response.Write("<script>alert('分数大于了100分,请重新选择！')</script>");
            }
        }

    }
    public void Rscore()
    {

        double a = Convert.ToDouble(SingleNum.Text);//定义一个变量a，把TextBox1中的值赋给它
        double b = Convert.ToDouble(MultiNum.Text);
        double c = Convert.ToDouble(JudgeNum.Text);
        double d = Convert.ToDouble(JianDaNum.Text);
        double e = Convert.ToDouble(LunShuNum.Text);
        double a_f = Convert.ToDouble(SingleFen.Text);//定义一个变量a，把TextBox1中的值赋给它
        double b_f = Convert.ToDouble(MultiFen.Text);
        double c_f = Convert.ToDouble(JudgeFen.Text);
        double d_f = Convert.ToDouble(JianDaFen.Text);
        double e_f = Convert.ToDouble(LunShuFen.Text);
        Label21.Text = Convert.ToString(a * a_f + b * b_f + c * c_f + d * d_f);

    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        //Datacon db = new Datacon();
        //string insertpaper = "insert into TaoTi(c_id,PaperName,PaperState,JoinTime,AnswerTime,Score,teacher_id) values(" + int.Parse(ddlCourse.SelectedValue) + ",'" + txtPaperName.Text + "','" + DropDownList2.SelectedValue + "','" + DateTime.Now.ToString() + "','" + DropDownList1.SelectedValue + "', '" + Label21.Text + "','" + Session["ID"] + "')";
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            TaoTi p = new TaoTi();
            p.c_id = int.Parse(ddlCourse.SelectedValue);
            p.PaperName = txtPaperName.Text;
            p.PaperState =Convert.ToString( DropDownList2.SelectedValue);
            p.JoinTime = DateTime.Now.ToString();
            p.AnswerTime = Convert.ToString(DropDownList1.SelectedValue);
            p.Score = Convert.ToInt32(Label21.Text.Trim());
            ds.TaoTi.InsertOnSubmit(p);
            ds.SubmitChanges();
            //int afterID = db.GetIDInsert(insertpaper);//保存试卷，并返回自动生成的试卷编号
            int afterID = p.ID;
            if (afterID > 0)
            {
                foreach (GridViewRow dr in GridView1.Rows)//保存试卷单选题信息
                {
                    //string single = "insert into TaoTiDetail(PaperID,Type,TitleID,Mark) values(" + afterID + ",'单选题'," + int.Parse(((Label)dr.FindControl("Label3")).Text) + "," + int.Parse(SingleFen.Text) + ")";
                    //db.Insert(single);
                    TaoTiDetail p2 = new TaoTiDetail();
                    p2.PaperID = afterID;
                    p2.Type = "单选题";
                    p2.TitleID = int.Parse(((Label)dr.FindControl("Label3")).Text);
                    p2.Mark = int.Parse(SingleFen.Text);
                    ds.TaoTiDetail.InsertOnSubmit(p2);
                }
                foreach (GridViewRow dr in GridView2.Rows)//保存试卷多选题信息
                {
                    //string multi = "insert into TaoTiDetail(PaperID,Type,TitleID,Mark) values(" + afterID + ",'多选题'," + int.Parse(((Label)dr.FindControl("Label6")).Text) + "," + int.Parse(MultiFen.Text) + ")";
                    //db.Insert(multi);
                    TaoTiDetail p3 = new TaoTiDetail();
                    p3.PaperID = afterID;
                    p3.Type = "多选题";
                    p3.TitleID = int.Parse(((Label)dr.FindControl("Label6")).Text);
                    p3.Mark = int.Parse(MultiFen.Text);
                    ds.TaoTiDetail.InsertOnSubmit(p3);
                }
                foreach (GridViewRow dr in GridView3.Rows)//保存试卷判断题信息
                {
                    //string judge = "insert into TaoTiDetail(PaperID,Type,TitleID,Mark) values(" + afterID + ",'判断题'," + int.Parse(((Label)dr.FindControl("Label7")).Text) + "," + int.Parse(JudgeFen.Text) + ")";
                    //db.Insert(judge);
                    TaoTiDetail p4 = new TaoTiDetail();
                    p4.PaperID = afterID;
                    p4.Type = "判断题";
                    p4.TitleID = int.Parse(((Label)dr.FindControl("Label7")).Text);
                    p4.Mark = int.Parse(JudgeFen.Text);
                    ds.TaoTiDetail.InsertOnSubmit(p4);
                }
                foreach (GridViewRow dr in GridView4.Rows)//保存试卷简答题信息
                {
                    //string judge = "insert into TaoTiDetail(PaperID,Type,TitleID,Mark) values(" + afterID + ",'判断题'," + int.Parse(((Label)dr.FindControl("Label7")).Text) + "," + int.Parse(JudgeFen.Text) + ")";
                    //db.Insert(judge);
                    TaoTiDetail p5 = new TaoTiDetail();
                    p5.PaperID = afterID;
                    p5.Type = "简答题";
                    p5.TitleID = int.Parse(((Label)dr.FindControl("Label28")).Text);
                    p5.Mark = int.Parse(JianDaFen.Text);
                    ds.TaoTiDetail.InsertOnSubmit(p5);
                }
                foreach (GridViewRow dr in GridView5.Rows)//保存试卷简答题信息
                {
                    //string judge = "insert into TaoTiDetail(PaperID,Type,TitleID,Mark) values(" + afterID + ",'判断题'," + int.Parse(((Label)dr.FindControl("Label7")).Text) + "," + int.Parse(JudgeFen.Text) + ")";
                    //db.Insert(judge);
                    TaoTiDetail p5 = new TaoTiDetail();
                    p5.PaperID = afterID;
                    p5.Type = "论述题";
                    p5.TitleID = int.Parse(((Label)dr.FindControl("Label31")).Text);
                    p5.Mark = int.Parse(LunShuFen.Text);
                    ds.TaoTiDetail.InsertOnSubmit(p5);
                }

                ds.SubmitChanges();
            }
        }
        Response.Write("<script>alert('添加成功！')</script>");
        Response.Write("<script>window.location.href='AddTaoTi.aspx'</script>");
    }
}